home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6026 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Do you have ever pass structures?
  5. Date: 21 Feb 1996 14:59:44 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gg850INNi4k@keats.ugrad.cs.ubc.ca>
  8. References: <4ge8mi$qjm@srvr1.engin.umich.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ge8mi$qjm@srvr1.engin.umich.edu>,
  12. HASDI RODZMANN HASHIM <hasdi@news-server.engin.umich.edu> wrote:
  13. >As I gain more and more experience in writing C code (sure), I find
  14. >myself passing more and more information and returning even more.
  15. >This is where using structures become handy.
  16. >
  17. >MY QUESTION is do you ever pass the whole structures to a function
  18. >or you just pass a pointer to a function? For example, for a struct
  19.              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20.  
  21.        [ [ pass ]V [ a pointer ]NP [ to a function ]PP ]VP
  22.  
  23.        [ [ pass ]V [ a pointer [ to a function ]PP ]NP ]VP
  24.  
  25. Ambigous natural language: I love it!
  26.  
  27. >If I want to avoid bar from being modified, one limited way to do
  28. >it is to add the CONST qualifier.
  29. >    void    foo(const bar *a)
  30. >
  31. >So is there any REAL advantage is passing an entire structure? Do people
  32. >ever do it?
  33.  
  34. The const qualifier makes no real guarantee that the object won't be modified,
  35. accidentally. In most C environments, you could still define a function foo()
  36. which clobbers the structure, and have it link in without a hitch.
  37.  
  38. The idea passing structures by value is that you _can_ modify the parameter,
  39. _without_ affecting the argument. For small structures, it may be worth-while,
  40. if your function can benefit from these semantics. 
  41.  
  42. Structure passing is typically implemented by putting the whole damn thing on
  43. the stack, so it's probably not the best thing for large structures,
  44. particularly if a pointer will suffice.
  45.  
  46. >I've some people's source code and almost always, they
  47. >pass pointers to structures instead of the structure itself. In a way,
  48.  
  49. Some old compilers may not like structure passing. Also, passing pointers to
  50. structures makes sense when the called function is supposed to do something to
  51. the structure, which is often the case. For example, it would be no use passing
  52. a FILE structure to fread(), since fread() needs to update the fields of the
  53. FILE structure such as the buffer pointer.
  54.  
  55. Pointers to structures are often used as handles for abstract data types. The
  56. code using the pointer may not even have a complete declaration for the
  57. structure, which is hidden in another module.
  58. -- 
  59.  
  60.